﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.Utils;


using MM.Entities;
using MM.Business;

using Word1 = Microsoft.Office.Interop.Word;
using System.Reflection;


namespace MM
{
    public partial class frmInKehoachBQBD : Form
    {
        string _matau = "";
        string _nguoitao = "";
        string _maphieuyeucau = "";
        string _sConn = "";
        string[] arrThamSo = new string[10];

        public frmInKehoachBQBD(string sConn)
        {
            InitializeComponent();
            _matau = frmDangNhap.objTau._tenTau2KiTu;
            _nguoitao = frmDangNhap.objTaiKhoan._soCMND;                      
            _sConn = sConn;

            this.InitThamSo();
        }

        public void InitThamSo()
        {
            THAM_SO objTS = new THAM_SO();
            clsTHAM_SO tsCtrl = new clsTHAM_SO(_sConn);
            objTS = tsCtrl.SelectTHAM_SO("vThuocTinh");
            if (objTS != null && objTS.GiaTri != "")
            {
                arrThamSo = objTS.GiaTri.Split(',');
            }
        }

        private void HienthiPHIEU_YEU_CAU()
        {
            clsPHIEU_YEU_CAU pycCtrl = new clsPHIEU_YEU_CAU(_sConn);
            List<PHIEU_YEU_CAU> lst = new List<PHIEU_YEU_CAU>();
            lst = pycCtrl.SelectPHIEU_YEU_CAU(_maphieuyeucau);
            if (lst.Count > 0)
            {
                this.txtMaPhieuYeuCau.Text = lst[0].MaPhieuYeuCau;
                this.txtNguoiThucHien.Text = lst[0].NguoiTao;
                this.txtNgayThucHien.Text = lst[0].NgayTao.Day.ToString() + "/" + lst[0].NgayTao.Month.ToString() + "/" + lst[0].NgayTao.Year.ToString();
                if (lst[0].DaDuyet == 1)
                {
                    chkDaduyet.Checked = true;
                    this.cmdChon.Enabled = true;
                }
                else
                {
                    chkDaduyet.Checked = false;
                    this.cmdChon.Enabled = false;
                }

                if (lst[0].TinhTrang == 1)
                {
                    chkXoa.Checked = true;
                }
                else
                {
                    chkXoa.Checked = false;
                }
            }
        }
        private void HienthiCHI_TIET_PYC()
        {
            List<CHI_TIET_PYC> lstCT = new List<CHI_TIET_PYC>();

            clsCHI_TIET_PYC ctCtrl = new clsCHI_TIET_PYC(_sConn);
            clsCHI_TIET_PDH_COMPONENT comCtrl = new clsCHI_TIET_PDH_COMPONENT(_sConn);
            List<CHI_TIET_PDH_COMPONENT> lstCom;

            lstCT = ctCtrl.SelectCHI_TIET_PYCByMaPhieuYeuCau(_maphieuyeucau);
            for (int i = 0; i < lstCT.Count; i++)
            {
                if (lstCT[i].MaTrangThai == 1)// gia cong
                {
                    lstCom = new List<CHI_TIET_PDH_COMPONENT>();
                    lstCom = comCtrl.SelectCHI_TIET_PYC_COMPONENT(lstCT[i].MaChiTiet);
                    lstCT[i].lstComponent = lstCom;
                }
            }
            this.gridControl1.DataSource = lstCT;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            frmTimPhieuYC f = new frmTimPhieuYC(_sConn);
            f.chonPYC += new frmTimPhieuYC.ChonPhieuYeuCau(GetPhieuYeuCau);
            f.Show(this);

        }
        private void GetPhieuYeuCau(object sender, ChonPhieuYCEventArgs e)
        {
            _maphieuyeucau = e.MaPhieuYeuCau;


            this.HienthiPHIEU_YEU_CAU();
            this.HienthiCHI_TIET_PYC();
           
        }

        private void cmdChon_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtMaPhieuYeuCau.Text != "" && this.txtMaPhieuYeuCau.Text.Length == 10)
                {
                    if (this.cboPYCChon.Items.IndexOf(this.txtMaPhieuYeuCau.Text)<0)
                        this.cboPYCChon.Items.Add(this.txtMaPhieuYeuCau.Text);
                }
            }
            catch { }
        }

        private void cmdXoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.cboPYCChon.Items.Count > 0)
                {
                    this.cboPYCChon.Items.RemoveAt(this.cboPYCChon.SelectedIndex);
                    this.cboPYCChon.Text = "";
                }
                this.cboPYCChon.Refresh();
            }
            catch { }
        }

        private void cboPYCChon_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _maphieuyeucau = this.cboPYCChon.Text;
                if (_maphieuyeucau != "")
                {
                    this.HienthiCHI_TIET_PYC();
                    this.HienthiPHIEU_YEU_CAU();
                }
            }
            catch { }
        }

        private void cmdXuatFile_Click(object sender, EventArgs e)
        {
            WaitDialogForm dlg;
            dlg = new WaitDialogForm("Đang xuất Báo cáo...", "");
            try
            {
                if (cboPYCChon.Items.Count == 0)
                {
                    dlg.Close();
                    dlg.Dispose();
                    MessageBox.Show("VUI LÒNG CHỌN PHIẾU YÊU CẦU ĐỂ XUẤT FILE KẾ HOẠCH");
                    return;
                }
              

                object oMissing = System.Reflection.Missing.Value;
                //Start Word and create a new document.
                Word1._Application oWord;
                Word1._Document oDoc;

                oWord = new Word1.Application();
                oWord.Visible = false;

                string templatePath = Path.GetDirectoryName(Application.ExecutablePath) + @"\10-01-GE-02.dot";

                object oTemplate = templatePath;
                oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
                    ref oMissing, ref oMissing);

                object oBookMark = "bTable";

                Word1.Table oTable;
                Word1.Range wrdRng = oDoc.Bookmarks.get_Item(ref oBookMark).Range;


                int rows = 0;
                int cols = 6;

                clsTHUOC_TINH ttCtrl = new clsTHUOC_TINH(_sConn);
                List<THUOC_TINH> lstTT = new List<THUOC_TINH>();
                List<CHI_TIET_PYC> lst = new List<CHI_TIET_PYC>();
                List<CHI_TIET_PYC> lstTemp = new List<CHI_TIET_PYC>();
                CHI_TIET_PYC obj;
                clsCHI_TIET_PYC pycCtrl = new clsCHI_TIET_PYC(_sConn);
                for (int i = 0; i < cboPYCChon.Items.Count; i++)
                {
                    // lay du lieu chi tiet phieu yeu cau
                    lstTemp.Clear();
                    lstTemp = pycCtrl.SelectCHI_TIET_PYCByMaPhieuYeuCau(cboPYCChon.Items[i].ToString());
                    for (int j = 0; j < lstTemp.Count; j++)
                    {
                        obj = new CHI_TIET_PYC();
                        obj = lstTemp[j];

                        lstTT.Clear();
                        lstTT = ttCtrl.SelectTHUOC_TINHByID(obj.MaThietBi);
                        foreach (THUOC_TINH t in lstTT)
                        {
                            if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                            {
                                if (t._giaTriTT != "" && t._giaTriTT != null)
                                {
                                    if (t._giaTriTT!="")
                                        obj.MaThietBi += "\n" + t._tenLoaiTT +":" +t._giaTriTT;
                                }
                            }
                        }

                        lst.Add(obj);
                    }

                }

                rows = lst.Count;

                oTable = oDoc.Tables.Add(wrdRng, rows, cols, ref oMissing, ref oMissing);
                oTable.Range.ParagraphFormat.SpaceAfter = 6;

                int r = 0, c = 0;
                string strText;


                int k = 0;
                for (r = 3; r <= lst.Count + 2; r++)
                {
                    for (c = 1; c <= cols; c++)
                    {
                        //strText = "r" + r + "c" + c;
                        //oTable.Cell(r, c).Range.Text = strText;
                        if (c == 1)
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.37f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                        if (c == 2)
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.59f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                        if (c == 3)
                        {
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(2.26f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                            oTable.Cell(r, c).Range.Text = lst[k].TenVTPT;
                        }
                        if (c == 4)
                        {
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(2.07f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                            oTable.Cell(r, c).Range.Text = lst[k].MaThietBi ;
                        }
                        if (c == 5)
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.89f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                        if (c == 6)
                        {
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.78f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                            oTable.Cell(r, c).Range.Text = lst[k].SoLuong.ToString();
                        }

                        oTable.Cell(r, c).Range.Borders[Word1.WdBorderType.wdBorderLeft].LineStyle = Word1.WdLineStyle.wdLineStyleSingle;
                        oTable.Cell(r, c).Range.Borders[Word1.WdBorderType.wdBorderBottom].LineStyle = Word1.WdLineStyle.wdLineStyleDot;
                        oTable.Cell(r, c).Range.Borders[Word1.WdBorderType.wdBorderRight].LineStyle = Word1.WdLineStyle.wdLineStyleSingle;
                        oTable.Cell(r, c).Range.Font.Name = "VNI-Times";
                        oTable.Cell(r, c).Range.Font.Size = 10;
                        oTable.Cell(r, c).Range.Font.Underline = 0;
                    }
                    k++;
                }

                oTable.Rows[rows + 2].Range.Borders[Word1.WdBorderType.wdBorderBottom].LineStyle = Word1.WdLineStyle.wdLineStyleSingle;

                oBookMark = "bPYC";
                string txt = "Danh muïc VTPT yeâu caàu ñöôïc trích töø caùc phieáu:";
                for (int j = 0; j < cboPYCChon.Items.Count; j++)
                {
                    txt += " " + cboPYCChon.Items[j].ToString() +";";
                }

                oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = txt;

                dlg.Close();
                dlg.Dispose();

                oWord.Visible = true;
            }
            catch
            {
                dlg.Close();
                dlg.Dispose();
            }
        }

        private void frmInKehoachBQBD_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }

        private void txtMaPhieuYeuCau_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                _maphieuyeucau = this.txtMaPhieuYeuCau.Text;
                this.HienthiPHIEU_YEU_CAU();
                this.HienthiCHI_TIET_PYC();
            }
            catch { }
        }

        private void frmInKehoachBQBD_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.F) // tim phieu yeu cau
            {
                label1_Click(null, null);
            }
        }

        private void cmdXuatFilePYC_Click(object sender, EventArgs e)
        {
            WaitDialogForm dlg;
            dlg = new WaitDialogForm("Đang xuất Báo cáo...", "");

            try
            {
                if (cboPYCChon.Items.Count == 0)
                {
                    dlg.Close();
                    dlg.Dispose();
                    MessageBox.Show("VUI LÒNG CHỌN PHIẾU YÊU CẦU ĐỂ XUẤT FILE YÊU CẦU VTPT");
                    return;
                }

              

                object oMissing = System.Reflection.Missing.Value;
                //Start Word and create a new document.
                Word1._Application oWord;
                Word1._Document oDoc;

                oWord = new Word1.Application();
                oWord.Visible = false;

                string templatePath = Path.GetDirectoryName(Application.ExecutablePath) + @"\06-01-GE-01.dot";

                object oTemplate = templatePath;
                oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
                    ref oMissing, ref oMissing);

                object oBookMark = "bTable";

                Word1.Table oTable;
                Word1.Range wrdRng = oDoc.Bookmarks.get_Item(ref oBookMark).Range;


                int rows = 0;
                int cols = 8;

                clsTHUOC_TINH ttCtrl = new clsTHUOC_TINH(_sConn);
                List<THUOC_TINH> lstTT = new List<THUOC_TINH>();
                List<CHI_TIET_PYC> lst = new List<CHI_TIET_PYC>();
                List<CHI_TIET_PYC> lstTemp = new List<CHI_TIET_PYC>();
                CHI_TIET_PYC obj;
                clsCHI_TIET_PYC pycCtrl = new clsCHI_TIET_PYC(_sConn);
                for (int i = 0; i < cboPYCChon.Items.Count; i++)
                {
                    // lay du lieu chi tiet phieu yeu cau
                    lstTemp.Clear();
                    lstTemp = pycCtrl.SelectCHI_TIET_PYCByMaPhieuYeuCau(cboPYCChon.Items[i].ToString());
                    for (int j = 0; j < lstTemp.Count; j++)
                    {
                        obj = new CHI_TIET_PYC();
                        obj = lstTemp[j];

                        lstTT.Clear();
                        lstTT = ttCtrl.SelectTHUOC_TINHByID(obj.MaThietBi);
                        foreach (THUOC_TINH t in lstTT)
                        {
                            if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                            {
                                if (t._giaTriTT != "" && t._giaTriTT != null)
                                {
                                    if (t._giaTriTT != "")
                                        obj.MaThietBi += "\n" + t._tenLoaiTT + ":" + t._giaTriTT;
                                }
                            }
                        }

                        lst.Add(obj);
                    }
                }

                rows = lst.Count;

                oTable = oDoc.Tables.Add(wrdRng, rows, cols, ref oMissing, ref oMissing);
                oTable.Range.ParagraphFormat.SpaceAfter = 6;

                int r = 0, c = 0;
                string strText;


                int k = 0;
                for (r = 1; r <= lst.Count ; r++)
                {
                    for (c = 1; c <= cols; c++)
                    {
                      
                        if (c == 1)
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.38f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                        if (c == 2)
                        {
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(1.29f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                            oTable.Cell(r, c).Range.Text = lst[k].TenVTPT;
                        }
                        if (c == 3)
                        {
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.53f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                            oTable.Cell(r, c).Range.Text = lst[k].DonViTinh;
                        }
                        if (c == 4)
                        {
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.88f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                            oTable.Cell(r, c).Range.Text = lst[k].MaThietBi;
                        }
                        if (c == 5)
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(1.13f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                        if (c == 6)
                        {
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(1.06f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                            oTable.Cell(r, c).Range.Text = lst[k].SoLuong.ToString();
                        }
                        if (c == 7)
                        {
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.94f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                            
                        }
                        if (c == 8)
                        {
                            oTable.Cell(r, c).SetWidth(oWord.InchesToPoints(0.83f), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                            oTable.Cell(r, c).Range.Text = lst[k].GhiChu;
                        }
                        oTable.Cell(r, c).Range.Borders[Word1.WdBorderType.wdBorderLeft].LineStyle = Word1.WdLineStyle.wdLineStyleSingle;
                        oTable.Cell(r, c).Range.Borders[Word1.WdBorderType.wdBorderBottom].LineStyle = Word1.WdLineStyle.wdLineStyleDot;
                        oTable.Cell(r, c).Range.Borders[Word1.WdBorderType.wdBorderRight].LineStyle = Word1.WdLineStyle.wdLineStyleSingle;
                        oTable.Cell(r, c).Range.Font.Name = "VNI-Times";
                        oTable.Cell(r, c).Range.Font.Size = 10;
                        oTable.Cell(r, c).Range.Font.Underline = 0;
                    }
                    k++;
                }

                oTable.Rows[rows ].Range.Borders[Word1.WdBorderType.wdBorderBottom].LineStyle = Word1.WdLineStyle.wdLineStyleSingle;
                
                oBookMark = "bPYC";
                string txt = "Danh mục VTPT yêu cầu trích từ phiếu yêu cầu: ";
                for (int j = 0; j < cboPYCChon.Items.Count; j++)
                {
                    txt += " " + cboPYCChon.Items[j].ToString() + ";";
                }

                oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = txt;

                dlg.Close();
                dlg.Dispose();

                oWord.Visible = true;
            }
            catch(Exception ex) 
            {
                dlg.Close();
                dlg.Dispose();
                MessageBox.Show("LỖI: "+ ex.Message);
            }
        }

    }
}